layui.define(['layer'],function(exports){
	var $ = layui.$,
		layer = layui.layer,
		bindLoading = {
			ins: null,
			config: null,
			loading: function(elem,options){
				this.ins = $(elem);
				//默认配置
				options = options || new Object();
				options.type = options.type || 0;//loading类型
				options.point = options.point || {show:false};//提示语{show:true,content:'加载中',style:""}
				options.times = options.times || -1;//单位ms
				options.zindex = options.zindex || 19891015;
				this.config = options;
				this.initialize();
			},
			initialize: function(){
				var loading = this.ins.find('.layui-layer-bind > .layui-layer-bind-content > div');
				if(loading.length){
					this.loadClassChange(loading);
				}else{
					this.render();
				}
				//定时关闭
				this.timeOutClose(this.config.times);
			},
			render: function(){
				var loadHtml = $(['<div class="layui-layer-bind layui-layer-loading" style="z-index:'+this.config.zindex+'">',
					'<div class="layui-layer-bind-content">',
					'<div class="layui-layer-bind-loading'+this.config.type+'"></div></div></div>'].join(''));
				if(this.ins.css("position")=='static')this.ins.css("position","relative");
                this.ins.append(loadHtml);
				this.loadStyle(this.ins.find('.layui-layer-bind > .layui-layer-bind-content'),this.config.point);
			},
			loadClassChange: function(loading){
				if(!loading.hasClass("layui-layer-bind-loading"+this.config.type)){
					loading.removeClass(loading[0].className).addClass("layui-layer-bind-loading"+this.config.type);
				}
				this.loadStyle(loading.parent(),this.config.point);
			},
			loadStyle: function(content,point){
				var pointSpan = content.find('.layui-layer-bind-point');
				if(point.show){
					if(pointSpan.length){
						pointSpan.text(point.content);
					}else{
						var pointHtml = '<span class="layui-layer-bind-point">';
							pointHtml += point.content?point.content:"加载中"+ '</span>';
						content.append(pointHtml);
						pointSpan = content.find('.layui-layer-bind-point');
					}
					pointSpan.attr('style',point.style || "");
				}else{
					pointSpan.remove();
				}
			},
			timeOutClose: function(times){
				if(times === -1) return;
				var elem = '#' + this.ins.attr('id');
				setTimeout(function(){
					bindLoading.close(elem);
				},times)
			},
			close: function(elem){
				$(elem).find(".layui-layer-bind").remove();
			},
			closeAll: function(){
				$(".layui-layer-bind").remove();
			}
	};
	
	layui.link(layui.cache.base+'bindLoading/bindLoading.css');
	exports("bindLoading",bindLoading);
})

